<?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Log\Writer;
use Illuminate\Support\ServiceProvider;
use Monolog\Logger;

class LogServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('log', function () {
            return $this->createAppLogger();
        });
//        $this->app->singleton('sql-log', function () {
//            return $this->createSqlLogger();
//        });
    }

    public function createAppLogger()
    {
        $log = new Writer(
            new Logger($this->channel()), $this->app['events']
        );
        $this->configureHandler($log, 'app');
        return $log;
    }

    /**
     * 创建sql日志生成器
     */
    public function createSqlLogger()
    {
        $log = new Writer(
            new Logger($this->channel()), $this->app['events']
        );
        $this->configureHandler($log, 'sql');
        return $log;
    }

    /**
     * 日志环境
     * @return string
     */
    protected function channel()
    {
        return $this->app->bound('env') ? $this->app->environment() : 'production';
    }

    /**
     * 配置处理器
     * @param Writer $log
     * @param $base_name
     */
    protected function configureHandler(Writer $log, $base_name)
    {
        $this->{'configure' . ucfirst($this->handler()) . 'Handler'}($log, $base_name);
    }

    /**
     * 单文件日志处理器
     * @param Writer $log
     * @param $base_name
     */
    protected function configureSingleHandler(Writer $log, $base_name)
    {
        $log->useFiles(
            sprintf('%s/logs/%s/%s%s.log', $this->app->storagePath(), $base_name, $this->getFilePrefix(), $base_name),
            $this->logLevel()
        );
    }

    /**
     *
     * @param Writer $log
     * @param $base_name
     */
    protected function configureDailyHandler(Writer $log, $base_name)
    {
        $log->useDailyFiles(
            sprintf('%s/logs/%s/%s%s.log.%s', $this->app->storagePath(), $base_name, $this->getFilePrefix(), $base_name, Carbon::now()->format('Ymd')),
            $this->maxFiles(),
            $this->logLevel()
        );
    }

    protected function configureSysLogHandler(Writer $log, $base_name)
    {
        $log->useSyslog($base_name, $this->logLevel());
    }

    protected function configureErrorLogHandler(Writer $log, $base_name)
    {
        $log->useErrorLog($this->logLevel());
    }

    protected function handler()
    {
        if ($this->app->bound('config')) {
            return $this->app->make('config')->get('app.log', 'single');
        }
        return 'single';
    }

    protected function logLevel()
    {
        if ($this->app->bound('config')) {
            return $this->app->make('config')->get('app.log_level', 'debug');
        }

        return 'debug';
    }

    protected function maxFiles()
    {
        if ($this->app->bound('config')) {
            return $this->app->make('config')->get('app.log_max_files', 5);
        }

        return 0;
    }

    protected function getFilePrefix()
    {
        return php_sapi_name() == 'cli' ? 'cli_' : '';
    }


}
